package date;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateTimeDemo {
    // 第三代日期类 线程安全
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println(ldt.getMinute());
        System.out.println(ldt.getMonthValue());

        //format 对象进行格式化
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss");
        String format = dtf.format(ldt);
        System.out.println(format);

        //instant timestamp
        Instant now = Instant.now();
        Date date = Date.from(now);
        System.out.println(now);

        //plus
        LocalDateTime ldt2 = ldt.plusDays(100);
        System.out.println(ldt2);
        ldt2 = ldt.minusMinutes(2000);
        System.out.println(ldt2);

        // 解析字符串时间 dateTimeFormatter要对应
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String test = "2024-07-29 12:00:31";
        LocalDateTime parse = LocalDateTime.parse(test, dtf2);
        String test1 = "2024-07-30 12:00:00";
        LocalDateTime parse1 = LocalDateTime.parse(test1, dtf2);
        System.out.println(parse1.isAfter(parse));
        System.out.println(parse);
    }
}
